package com.changfen.config;

import feign.Request;
import feign.Retryer;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.gson.GsonEncoder;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.TimeUnit;


public class GsonFeignConfiguration {
    @Bean
    Encoder GsonEncoder(){
        return new GsonEncoder();
    }

    @Bean
    Retryer retryer(){
        return new Retryer.Default();
    }

    @Bean
    Request.Options options(){
        return new Request.Options(2, TimeUnit.SECONDS,5,TimeUnit.SECONDS,true);
    }

    /**
     * feign全局处理
     * @return
     */
    @Bean
    ErrorDecoder errorDecoder(){
        return (s, response) -> {
            if(response.status() >=400 && response.status() <=499){
                return new Exception(response.reason());
            }
            return null;
        };
    }
}
